/*
 * chunk_manager_unittest.c
 *
 *  Created on: Mar 2, 2013
 *      Author: redchen
 */

#include <lfs.h>
#include <strings.h>
#include <stdio.h>
#include <string.h>
#include <unittest.h>
#include <stddefine.h>
#include <iterator_utils.h>
#include <emalloc.h>
#include <timer.h>
#include <store_file.h>

#include "chunk_manager.h"

int test_chunk_manager_setup()
{
    system("rm c.12000");

    chunk_manager_init();
    return 1;
}

int test_chunk_manager_cleanup()
{
    return 1;
}

void test_chunk_manager_chunk_create()
{
    Status s;
    s = chunk_manager_chunk_create("", 12000, "index.html", 360);
    LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_SUCC), lfs_msg(s));
    s = chunk_manager_chunk_create("", 12000, "index.html", 64 * 1024);
    LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_FILE_IS_EXIST), lfs_msg(s));
}

void test_chunk_manager_chunk_append()
{
    Status s;
    s = chunk_manager_chunk_append(12000, "123456789", 10);
    LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_SUCC), lfs_msg(s));
    s = chunk_manager_chunk_append(12000, "987654321", 10);
    LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_SUCC), lfs_msg(s));
    s = chunk_manager_chunk_append(12000, "089123456", 10);
    LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_LACK_OF_RESOURCE), lfs_msg(s));
}

void test_chunk_manager_chunk_read()
{
    Status s;
    Vector* files = vector_new();
    s = chunk_manager_chunk_read(12000, 0, timer_get_micro_second() + 1000 * 1000, files);
    LEMON_TEST_EQUAL_INT(2, vector_size(files));
    {
        BFile* f = vector_get(files, 0);
        LEMON_TEST_EQUAL_STRING("123456789", f->data);
    }
    {
        BFile* f = vector_get(files, 1);
        LEMON_TEST_EQUAL_STRING("987654321", f->data);
    }
}

void chunk_manager_add_suite()
{
    UNITTEST_SUITE_INIT();
    UNITTEST_SUITE_SETUP(test_chunk_manager_setup);
    UNITTEST_SUITE_CLEANUP(test_chunk_manager_cleanup);
    UNITTEST_SUITE_ADD_TESTCASE("test_chunk_manager_chunk_create", test_chunk_manager_chunk_create);
    UNITTEST_SUITE_ADD_TESTCASE("test_chunk_manager_chunk_append", test_chunk_manager_chunk_append);
    UNITTEST_SUITE_ADD_TESTCASE("test_chunk_manager_chunk_read", test_chunk_manager_chunk_read);
}

